home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4505 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.6 KB  |  120 lines

  1. Path: maine.maine.edu!rwl380b
  2. Organization: University of Maine System
  3. Date: Tue, 30 Jan 1996 10:59:40 EST
  4. From: <RWL380B@MAINE.MAINE.EDU>
  5. Message-ID: <96030.105940RWL380B@MAINE.MAINE.EDU>
  6. Newsgroups: comp.lang.c++
  7. Subject: Help with float/double data types
  8.  
  9. C++ Experts,
  10.  
  11. I have a "simple" question re: floating point data types in C/C++.
  12. I've traditionally programmed in Fortran but am trying to learn
  13. new tricks....  I ran into this problem while trying to write a
  14. filter that converts the output of one commercial program to the
  15. input format for Arc/Info.  A simplified version (just I/O) is
  16. included to illustrate my problems.  Variables of interest in
  17. this discussion are: X, Y, Z, Zstd.  This version of the program
  18. has been compiled in Turbo C++ v3.0 for DOS (Borland) and I've
  19. tried in Turbo C++ v4.5 for Windows (as an EasyWin program).  The
  20. file I am reading is an ASCII version of interpolated values of
  21. a spatial process occurring across the US.  (The program code and
  22. first lines of input file or at bottom of message).
  23.  
  24. If I declare X, Y, Z and Zstd as float:
  25.     float X=0.0,Y=0.0,Z=0.0,Zstd=0.0;
  26. Then the output looks correct but the digits after the decimal points
  27. in X are incorrect (original first line X = -2082085.85000).  The
  28. first value of Y is incorrect, but the next values are correct!
  29.  
  30.  
  31.  X-Coordinate     Y-Coordinate       Z-Est     Z StdDev    n
  32. -2082085.87500    -2127847.00000    23.12121    1.77931    8
  33. -2082085.87500    -2047847.12500    23.13800    1.73910    8
  34. -2082085.87500    -1967847.12500    23.15039    1.69751    8
  35. -2082085.87500    -1887847.12500    23.15789    1.65532    8
  36. -2082085.87500    -1807847.12500    23.15990    1.61336    8
  37. -2082085.87500    -1727847.12500    32.19845    1.55000    8
  38. -2082085.87500    -1647847.12500    38.46202    1.49013    8
  39. -2082085.87500    -1567847.12500    32.17017    1.45502    8
  40. -2082085.87500    -1487847.12500    39.51803    1.40543    8
  41. -2082085.87500    -1407847.12500    47.27689    1.37395    8
  42.  
  43.  
  44. If I declare X,Y,Z and Zstd to be double then the output is
  45. correct.
  46.  
  47.  X-Coordinate     Y-Coordinate      Z-Est      Z StdDev    n
  48. -2082085.85000    -2127847.12500    23.12121    1.77931    8
  49. -2082085.85000    -2047847.12500    23.13800    1.73910    8
  50. -2082085.85000    -1967847.12500    23.15039    1.69751    8
  51. -2082085.85000    -1887847.12500    23.15789    1.65532    8
  52. -2082085.85000    -1807847.12500    23.15990    1.61336    8
  53. -2082085.85000    -1727847.12500    32.19845    1.55000    8
  54. -2082085.85000    -1647847.12500    38.46202    1.49013    8
  55. -2082085.85000    -1567847.12500    32.17017    1.45502    8
  56. -2082085.85000    -1487847.12500    39.51803    1.40543    8
  57. -2082085.85000    -1407847.12500    47.27689    1.37395    8
  58.  
  59. Coming from Fortran, this should not be happening.  Borland's
  60. documentation suggests that negative floats and doubles don't
  61. exist.  However, their web page has a technical document
  62. describing how floating points are represented and indicate
  63. that negative floats and doubles are OK.  I am puzzled
  64. by the behavior I describe above.  And yes, I need access to
  65. the X, Y and Z values as numbers for certain statistics I
  66. need to include so I can't just read them as strings.
  67. Any help is greatly Appreciated!
  68.  
  69.  
  70. // Program Reformat.cpp
  71. #include <iostream.h>
  72. #include <fstream.h>
  73.  
  74. int main()
  75. {
  76.   float X=0.0, Y=0.0, Z=0.0, Zstd=0.0;   // coordinates & estimates.
  77.   int N=0;
  78.   char linebuf[80];
  79.   fstream filin;
  80.  
  81.   filin.open("519070.blk", ios::in);
  82.   cout.precision(5);
  83.   cout.setf(ios::fixed | ios::showpoint);
  84.  
  85.  
  86.   for (i = 0; i < 10; i++ ) {
  87.     filin >> X >> Y >> Z >> Zstd >> N;
  88.     cout << X << '\t' << Y << '\t' << Z << '\t' << Zstd << '\t' << N << '\n';
  89.   }
  90.  
  91.   filin.close();
  92.   return 1;
  93. }
  94.  
  95.  
  96. INPUT FILE: 519070.blk
  97.  
  98.     -2082085.85000  -2127847.12500        23.12121         1.77931     8
  99.     -2082085.85000  -2047847.12500        23.13800         1.73910     8
  100.     -2082085.85000  -1967847.12500        23.15039         1.69751     8
  101.     -2082085.85000  -1887847.12500        23.15789         1.65532     8
  102.     -2082085.85000  -1807847.12500        23.15990         1.61336     8
  103.     -2082085.85000  -1727847.12500        32.19845         1.55000     8
  104.     -2082085.85000  -1647847.12500        38.46202         1.49013     8
  105.     -2082085.85000  -1567847.12500        32.17017         1.45502     8
  106.     -2082085.85000  -1487847.12500        39.51803         1.40543     8
  107.     -2082085.85000  -1407847.12500        47.27689         1.37395     8
  108. <2173 linese deleted>
  109.  
  110.  
  111. Thanks,
  112. Tim Jones
  113. Research Associate
  114. Dept of Wildlife Ecology
  115. University of Maine
  116. Orono, ME 04418
  117. tim@wlm2.umenfa.maine.edu
  118.  
  119. PS - sorry for the length of this message!
  120.